package com.ruoyi.web.controller.kaoyan;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kaoyan.domain.EducationCategory;
import com.ruoyi.kaoyan.service.EducationCategoryService;
import com.ruoyi.system.service.ISysFileInfoService;
import com.ruoyi.web.controller.kaoyan.vo.CategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 后台分类模块管理
 * @author admin
 */
@Api(tags = "后台分类管理", value = "后台分类管理")
@RestController
@RequestMapping("/education/category")
public class EducationCategoryController extends BaseController {

    @Autowired
    private EducationCategoryService categoryService;
    @Autowired
    private ISysFileInfoService fileInfoService;

    @ApiOperation("类目列表")
    @PostMapping("/list")
    public AjaxResult categoryList(String name){
        //只查询id，类目名称，类目图标
        LambdaQueryWrapper<EducationCategory> select = new LambdaQueryWrapper<EducationCategory>()
                .like(StringUtils.isNotBlank(name), EducationCategory::getName, name);
        //分页并拿到类目列表
        List<EducationCategory> categoryList = categoryService.list(select);
        List<CategoryVo> list = categoryList.stream().map(CategoryVo::new).collect(Collectors.toList());
        //映射子类目
        Map<Long, List<CategoryVo>> map = list.stream().collect(Collectors.groupingBy(CategoryVo::getPid));
        //获取图标地址
        List<String> idSet = categoryList.stream().map(EducationCategory::getIconGroupId).collect(Collectors.toList());
        Map<String, String> filePathMap = fileInfoService.getFilePathMap(idSet);
        List<EducationCategory> categories = categoryList.stream().filter(e -> "L1".equals(e.getCategoryLevel())).collect(Collectors.toList());
        if (StringUtils.isNotEmpty(categoryList) && StringUtils.isEmpty(categories)){
            categories = categoryList;
        }
        List<CategoryVo> categoryVoList = categories.stream().map(e -> new CategoryVo(e, filePathMap, map)).collect(Collectors.toList());
        return AjaxResult.ok().list(categoryVoList).build();
    }

    @ApiOperation("添加或修改类目")
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody EducationCategory category){
        if (StringUtils.checkObjAllFieldsIsNull(category)){
            return AjaxResult.error("不能为空");
        }
        boolean update = categoryService.saveOrUpdate(category);
        return toAjax(update);
    }

    @ApiOperation("删除类目")
    @PostMapping("/remove")
    public AjaxResult removeById(Long id){
        boolean remove = categoryService.removeById(id);
        return toAjax(remove);
    }
}
